<template>
	<view class="container">
		<view class="header" :style="headerStyle">
			<button class="btn-back" @click="onBackClick"><image src="../../../static/fanli/326.png" mode="" class="icon"></image></button>
		</view>
		
		<view class="header bgnone" :style="headerStyleDown">
			<button class="btn-back" @click="onBackClick"><image src="../../../static/fanli/325.png" mode="" class="icon2"></image></button>
		</view>
		
		<view class="banner">
			<image src="../../../static/fanli/310.jpg" mode="widthFix" class="pic"></image>
			
			<view class="step">
				<view class="bg1"></view>
				<view class="bg2"></view>
				<view class="ul">
					<view class="li">
						<text class="i">第一步</text>
						<text class="b">邀请3个好友</text>
					</view>
					<view class="li">
						<text class="i">第二步</text>
						<text class="b">成功注册</text>
					</view>
					<view class="li">
						<text class="i">第三步</text>
						<text class="b">下单购买</text>
					</view>
					<view class="li">
						<text class="i">第四步</text>
						<text class="b">返利到账户</text>
					</view>
				</view>
			</view>
			
			<view class="timeout">
				<text class="p">你的免单权益有效期还剩</text>
				
				<view class="con">
					<view class="dfn">
						<text>{{dayline[0]}}</text>
					</view>
					<view class="dfn mr10">
						<text>{{dayline[1]}}</text>
					</view>
					<text class="i">时</text>
					
					<view class="dfn ml20">
						<text>{{dayline[2]}}</text>
					</view>
					<view class="dfn mr10">
						<text>{{dayline[3]}}</text>
					</view>
					<text class="i">分</text>
					
					<view class="dfn ml20">
						<text>{{dayline[4]}}</text>
					</view>
					<view class="dfn mr10">
						<text>{{dayline[5]}}</text>
					</view>
					<text class="i">秒</text>
				</view>
			</view>
		</view>
		
		<view class="rule">
			<text></text>
			<view class="next" @click="onShowRulePopupClick">查看规则</view>
		</view>
		
		<view class="list_dt">
			<image src="../../../static/fanli/309.png" mode="widthFix" class="icon"></image>
		</view>
		
		<view class="list">
			<view v-for="(item, index) in list" :key="index" class="item">
				<image :src="item.main_pic" mode="" class="pic"></image>
				
				<image src="../../../static/fanli/308.png" mode="widthFix" class="icon"></image>
				
				<view class="cite">
					<text class="name">{{item.title}}</text>
					
					<view class="dfn">
						
						<view class="price">
							<text class="i">￥</text>
							<text>{{item.itemfee_final}}</text>
							<text class="u">原价￥{{item.itemfee}}</text>
						</view>
						
						<view class="row mt4">
							<view class="quan">
								<text>{{item.itemfee_coupon}}元券</text>
							</view>
							
							<button type="primary" class="btn" @click="onInviteClick" v-if="invite.length < 3">邀请好友</button>
							
							<button type="primary" class="btn" @click="onBuyClick(item, index)" v-if="invite.length >= 3">立即购买</button>
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<view class="bottom" v-if="invite.length">
			<image 
				v-for="(item, index) in invite"
				:key="index"
				:src="item" 
				mode="" 
				class="pic"
			></image>
			<text>{{invite.length >= 3 ? '恭喜您获得免单资格' : '已邀请' + invite.length + '个好友注册'}}</text>
		</view>
		
		<view class="popup_rule" v-if="showRulePopup">
			<view class="shadow" @click="onCloseRulePopupClick"></view>
			<view class="con">
				<view class="title">
					<text>新人免单规则</text>
				</view>
				<view class="p">
					<text>1. 邀请3个好友注册就可获得一次免单资格。</text>
				</view>
				<view class="p">
					<text> 2. 有效时间为72个小时，超过72小时自动取消免单资格。</text>
				</view>
				<view class="p">
					<text>3. 新人免单限未购买过商品的用户。</text>
				</view>
				<view class="p">
					<text>4. 确认收货后商品金额返还到账户余额中，可到余额中提现。</text>
				</view>
				<view class="p">
					<text> 5. 每个用户只有1次免单机会。</text>
				</view>
				<view class="close" @click="onCloseRulePopupClick">
					<image src="../../../static/fanli/313.png" mode="" class="icon"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { runGetV6Data } from '@/module/api'
    import WxNotificationCenter from '@/libs/WxNotificationCenter/wxnotificationcenter'
	import uniLoadMore from "@/components/uni-load-more/uni-load-more.vue"

	const _ = require("@/libs/underscore/underscore-min.js")._
	
	export default {
		data() {
			return {
				scrollTop: 0,
				statusBarHeight: 0,
				loaded: false,
				page: 1,
				loadingMore: true,
				isEmpty: false,
				list: [],
				showRulePopup: false,
				invite: [],
				dayline: [0,0,0,0,0,0]
			};
		},
		
		computed: {
			shareAppMessage () {
				return this.$store.state.shareAppMessage
			},
			
			userInfo () {
				return this.$store.state.userInfo
			},
			
			headerStyle () {
				let opacity = 0
				if(this.scrollTop > 0){
					opacity = this.scrollTop / 184
					if(opacity > 1) opacity = 1
				}
				return "opacity: " + opacity + ";padding-top: " + this.statusBarHeight + "px;"
			},
			
			headerStyleDown () {
				let opacity = 0
				if(this.scrollTop > 0){
					opacity = this.scrollTop / 184
					if(opacity > 1) opacity = 1
				}
				opacity = 1 - opacity
				return "opacity: " + opacity + ";padding-top: " + this.statusBarHeight + "px;"
			}
		},
		
		onPageScroll(e){
			this.scrollTop = e.scrollTop
		},
		
		onReady () {
			let _t = this
			uni.getSystemInfo({
				success: res => {
					// console.log(res)
					_t.statusBarHeight = res.statusBarHeight
				}
			})
		},
		
		onLoad() {
			this.getPageData()
		},
		
		onUnload() {
			this.timer && clearTimeout(this.timer);
		},
		
		methods:{
			updateDaylineTime (leftTime) {
				this.timer && clearTimeout(this.timer);
				let _t = this;
				let dayline = [0,0,0,0,0,0];
				//定义变量 d,h,m,s保存倒计时的时间
				var d, h, m, s;
				if (leftTime >= 0) {
					h = Math.floor(leftTime / 1000 / 60 / 60)
					m = Math.floor(leftTime / 1000 / 60 % 60)
					s = Math.floor(leftTime / 1000 % 60)
					
					h = h < 10 ? "0" + h : h.toString()
					m = m < 10 ? "0" + m : m.toString()
					s = s < 10 ? "0" + s : s.toString()
					
					h = h.split("")
					m = m.split("")
					s = s.split("")
					
					dayline[0] = h[0]
					dayline[1] = h[1]
					dayline[2] = m[0]
					dayline[3] = m[1]
					dayline[4] = s[0]
					dayline[5] = s[1]

					leftTime = leftTime - 1000;

					this.timer = setTimeout(() => {
						_t.updateDaylineTime(leftTime);
					}, 1000)
					
					this.dayline = dayline
				} else {
					this.dayline = dayline
				}
			},
			  
			onBuyClick (item, index) {
				let _t = this;
				let platform = '';
				let params = {
					act: 'linkMiniNew',
					uid: this.userInfo.id,
					itemid: item.item_id
				}
				
				switch (item.item_type) {
					case 3 :
						platform = 'jd'; 
						break;
					case 4 :
						platform = 'pdd';
						params.link_type = "wx_mini";
						break;
				}
				
				uni.showLoading({
					mask: true,
					title: '请求中...'
				})
				
				runGetV6Data({
					platform,
					params,
					success: res => {
						uni.hideLoading()
						
						switch (item.item_type) {
							case 3 :
								wx.navigateToMiniProgram({
									appId: 'wx13e41a437b8a1d2e',
									path: '/pages/jingfen_twotoone/item?spreadUrl=' + encodeURIComponent(res.data) + '&customerinfo=jyyx181029',
									extraData: {
										foo: 'bar'
									},
									envVersion: 'release',
									success(res) {
										// 打开成功
										console.log(res);
										console.log("打开成功")
									}
								})
								break;
							case 4 :
								let info = res.data.we_app_info;
								wx.navigateToMiniProgram({
									appId: info.app_id,
									path: info.page_path,
									envVersion: 'release',
									success(res) {
									  // 打开成功
									  console.log(res);
									  console.log("打开成功")
									}
								})
								break;
						}
					},
					fail: res => {
						uni.hideLoading()
						uni.showToast({
							icon: "none",
							title: res.msg
						})
					},
					error: () => {
						uni.hideLoading()
						uni.showToast({
							icon: "none",
							title: "系统繁忙，请稍候再试"
						})
					}
				})
			},
			
			onInviteClick () {
				uni.navigateTo({
					url: "/pageMe/me/invite-friends/invite-friends"
				})
			},
			
			onBackClick () {
				uni.navigateBack({
					
				})
			},
			
			getPageData () {
				let _t = this
				
				uni.showLoading({
					mask: true,
					title: '请求中...'
				})
				
				runGetV6Data({
					params: {
						act: 'miniFreeActivity',
						uid: this.userInfo.id
					},
					success: res => {
						uni.hideLoading()
						if(res.data.status == 1){
							_t.invite = res.data.head
							_t.list = res.data.data
							_t.updateDaylineTime(res.data.left ? res.data.left * 1000 : 0)
						}else{
							uni.showModal({
								content: "无参与活动资格",
								complete: () => {
									_t.onBackClick
								}
							})
						}
						
					},
					fail: res => {
						uni.hideLoading()
						uni.showModal({
							content: res.msg,
							complete: () => {
								_t.onBackClick
							}
						})
					},
					error: () => {
						uni.hideLoading()
						uni.showModal({
							content: "系统繁忙，请稍候再试",
							complete: () => {
								_t.onBackClick
							}
						})
					}
				})
			},
			
			onCloseRulePopupClick () {
				this.showRulePopup = false
			},
			
			onShowRulePopupClick () {
				this.showRulePopup = true
			}
		}
	}
</script>

<style>
	page{
		background: #FF3660;
	}
</style>

<style lang="less" scoped>
	.header{
		position: fixed;
		width: 100%;
		height: 92px;
		top: 0;
		left: 0;
		z-index: 10;
		opacity: 0;
		background: #FF5757;
		// background:linear-gradient(180deg,rgba(255,87,87,1) 0%,rgba(255,40,61,1) 100%);
		
		&.bgnone{
			background: none;
			opacity: 1;
		}
		
		.btn-back{
			width: 92px;
			height: 92px;
			display: flex;
			align-items: center;
			justify-content: center;
			background: none;
			
			.icon{
				width: 28px;
				height: 28px;
			}
			
			.icon2{
				width: 52px;
				height: 52px;
			}
		}
	}
	
	.banner{
		width: 100%;
		position: relative;
		
		.pic{
			width: 100%;
		}
		
		.step{
			position: absolute;
			width: 690px;
			left: 30px;
			bottom: -10px;
			
			.bg2{
				width: 94%;
				height: 24px;
				position: absolute;
				left: 3%;
				bottom: -8px;
				border-radius: 12px;
				background-color: #F2F2F2;
			}
			
			.bg1{
				width: 88%;
				height: 24px;
				position: absolute;
				left: 6%;
				bottom: -16px;
				border-radius: 12px;
				background-color: #F2F2F2;
				opacity: .9;
			}
			
			.ul{
				width: 100%;
				height: 142px;
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: center;
				background-color: #FFFFFF;
				border-radius: 12px;
				position: relative;
				
				.li{
					width: 25%;
					height: 100%;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					
					.i{
						font-size: 24px;
						color: #FF3660;
					}
					
					.b{
						font-size: 28px;
						color: #333;
						margin-top: 10px;
					}
				}
			}
		}
		
		.timeout{
			position: absolute;
			top: 384px;
			left: 0;
			width: 100%;
			display: flex;
			flex-direction: column;
			align-items: center;
			
			.p{
				font-size: 28px;
				color: #FFFFFF;
			}
			
			.con{
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: center;
				margin-top: 10px;
			
				.i{
					font-size: 28px;
					color: #FFFFFF;
				}
				
				.dfn{
					display: flex;
					flex-direction: row;
					align-items: center;
					justify-content: center;
					width: 28px;
					height: 52px;
					background: #222222;
					border-radius: 4px;
					margin: 0 2px;
					
					text{
						font-size: 32px;
						color: #FFFFFF;
					}
				}
				
				.mr10{
					margin-right: 10px;
				}
				
				.ml20{
					margin-left: 20px;
				}
			}
		}
	}
	
	.rule{
		width: 100%;
		box-sizing: border-box;
		padding: 50px 30px 0;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		
		.next{
			padding: 0 20px;
			height: 40px;
			border-radius: 20px;
			border: 1px solid #FFFFFF;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #FFFFFF;
			font-size: 26px;
		}
	}
	
	.list_dt{
		text-align: center;
		margin-top: 22px;
		
		.icon{
			width: 366px;
		}
	}
	
	.list{
		display: flex;
		align-items: center;
		flex-direction: column;
		width: 100%;
		margin-top: 30px;
		padding-bottom: 130px;
		
		.item{
			width: 690px;
			height: 272px;
			background-color: #FFFFFF;
			border-radius: 12px;
			overflow: hidden;
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-bottom: 20px;
			position: relative;
			
			.pic{
				height: 272px;
				width: 272px;
			}
			
			.icon{
				position: absolute;
				width: 56px;
				left: 20px;
				top: 0;
			}
			
			.cite{
				height: 272px;
				width: 418px;
				padding: 20px;
				box-sizing: border-box;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				
				.name{
					width: 100%;
					font-size: 28px;
					color: #333;
					display: -webkit-box;
					overflow: hidden;
					white-space: normal;
					text-overflow: ellipsis;
					word-wrap: break-word;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
				}
				
				.dfn{
					width: 100%;
					
					.price{
						display: flex;
						flex-direction: row;
						align-items: center;
						width: 100%;
						
						text{
							font-size: 32px;
							color: #FF3660;
						}
						
						.i{
							font-size: 24px;
							padding-top: 8px;
						}
						
						.u{
							font-size: 24px;
							padding-top: 8px;
							padding-left: 8px;
							text-decoration: line-through;
							color: #999999;
						}
					}
					
					.mt4{
						margin-top: 4px;
					}
					
					.row{
						display: flex;
						flex-direction: row;
						align-items: center;
						justify-content: space-between;
						
						.btn{
							width: 124px;
							height: 44px;
							background: rgba(255,54,96,1);
							border-radius: 6px;
							font-size: 26px;
							color: #FFFFFF;
							line-height: 44px;
						}
						
						.quan{
							background: #FFC42A;
							padding: 0 10px;
							height: 36px;
							display: flex;
							align-items: center;
							justify-content: center;
							position: relative;
							
							&::after{
								content: "";
								display: block;
								width: 10px;
								height: 100%;
								position: absolute;
								top: 0;
								right: 0;
								background: #FFFFFF url("../../../static/fanli/311.png") right center no-repeat;
								background-size: cover;
							}
							
							&::before{
								content: "";
								display: block;
								width: 10px;
								height: 100%;
								position: absolute;
								top: 0;
								left: 0;
								background: #FFFFFF url("../../../static/fanli/311.png") left center no-repeat;
								background-size: cover;
							}
							
							text{
								font-size: 22px;
								color: #FFFFFF;
							}
						}
					}
					
					.progress{
						justify-content: flex-start;
						margin-bottom: 20px;
						
						.line{
							width: 226px;
							height: 24px;
							background: rgba(255,228,234,1);
							border-radius: 12px;
							overflow: hidden;
							position: relative;
							
							.num{
								position: absolute;
								top: 0;
								right: 10px;
								line-height: 24px;
								font-size: 18px;
								color: #FF3660;
							}
							
							.inner{
								width: 200px;
								height: 100%;
								background: #FF3660;
								border-radius: 12px;
								overflow: hidden;
								
								.con{
									width: 226px;
									height: 100%;
									position: relative;
								}
								
								.num{
									color: #FFFFFF;
								}
							}
						}
						
						.u{
							font-size: 18px;
							color: #999999;
							margin-left: 10px;
						}
					}
				}
			}
		}
	}
	
	.bottom{
		position: fixed;
		bottom: 0;
		width: 100%;
		left: 0;
		height: 100px;
		background: rgba(0,0,0,.8);
		border-radius: 20px 20px 0px 0px;
		box-sizing: border-box;
		padding: 0 30px;
		display: flex;
		flex-direction: row;
		align-items: center;
		
		.pic{
			width: 76px;
			height: 76px;
			border-radius: 38px;
			margin-right: 20px;
		}
		
		text{
			font-size: 32px;
			color: #FFFFFF;
			padding-left: 20px;
		}
	}
	
	.popup_rule{
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		position: fixed;
		top: 0;
		left: 0;
		bottom: 0;
		right: 0;
		
		.shadow{
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			background: rgba(0,0,0,.75);
		}
		
		.con{
			position: relative;
			padding: 110px 40px 50px;
			box-sizing: border-box;
			width: 570px;
			background: linear-gradient(180deg,rgba(255,101,98,1) 0%,rgba(255,54,96,1) 100%);
			border-radius: 22px;
			
			.title{
				position: absolute;
				top: -28px;
				left: 136px;
				width: 298px;
				height: 120px;
				background: url(../../../static/fanli/312.png) center no-repeat;
				background-size: cover;
				display: flex;
				align-items: center;
				justify-content: center;
				
				text{
					font-size: 32px;
					color: #FFFFFF;
				}
			}
			
			.close{
				position: absolute;
				top: -186px;
				right: 18px;
				
				.icon{
					width: 76px;
					height: 76px;
				}
				
				&::after{
					height: 110px;
					width: 1px;
					background: #FFFFFF;
					position: absolute;
					right: 38px;
					top: 76px;
					display: block;
					content: "";
				}
			}
			
			.p{
				text{
					color: #FFFFFF;
					font-size: 28px;
				}
			}
		}
	}
</style>
